//
// Created by root on 6/22/17.
//

#include "menu.h"

/*
gboolean show_popup(GtkWidget *widget, GdkEvent *event)
{
            g_print ("show_popup 1.\n");
    const guint RIGHT_CLICK = 3;

    if (event->type == GDK_BUTTON_PRESS) {

        GdkEventButton *bevent = (GdkEventButton *) event;

        if (bevent->button == RIGHT_CLICK) {

            g_print ("show_popup - RIGHT_CLICK.\n");

            gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL,
                bevent->button, bevent->time);
        }

        return TRUE;
    }

    return FALSE;
}
*/

#define MNEMONIC_MENU_ITEM(_name,_mi) _mi = gtk_menu_item_new_with_mnemonic(_name)
#define IMAGE_MENU_ITEM(_stock,_mi)   _mi = gtk_image_menu_item_new_from_stock(GTK_STOCK_##_stock, NULL)
#define IMAGE_MENU_ITEM_LABLE(_stock,_mi,_label)   _mi = gtk_image_menu_item_new_from_stock(GTK_STOCK_##_stock, NULL); \
    gtk_menu_item_set_label(GTK_MENU_ITEM (_mi), _label)

#define SUBMENU(_super,_sub)          gtk_menu_item_set_submenu(GTK_MENU_ITEM (_sub), _super)
#define APPEND_SEPARATOR(_menu)       gtk_menu_shell_append(GTK_MENU_SHELL (_menu), gtk_separator_menu_item_new())
#define APPEND_MENU(_super,_sub)      gtk_menu_shell_append(GTK_MENU_SHELL (_super), _sub)
#define ADD_ACCELERATOR(_mi,_key,_mods,_flags)  gtk_widget_add_accelerator(_mi, "activate", mainaccel, _key,_mods,_flags)

#define ACTIVATE(_mi,_callback)     g_signal_connect (G_OBJECT(_mi), "activate", G_CALLBACK(_callback), app)

GtkWidget *create_menubar(FunnyWindow *app, GtkAccelGroup *mainaccel) {
    GtkWidget *menubar;         /* menu container   */
    GtkWidget *fileMenu;        /* file menu        */
    GtkWidget *fileMi;
    GtkWidget *newMi;
    GtkWidget *openMi;
    GtkWidget *saveMi;
    GtkWidget *saveasMi;
    GtkWidget *closeMi;
    GtkWidget *quitMi;

    GtkWidget *editMenu;        /* edit menu        */
    GtkWidget *editMi;
    GtkWidget *undoMi;
    GtkWidget *redoMi;
    GtkWidget *copyMi;
    GtkWidget *cutMi;
    GtkWidget *pasteMi;
    GtkWidget *deleteMi;
    GtkWidget *selallMi;
    GtkWidget *deselectMi;
    GtkWidget *findMi;
    GtkWidget *replaceMi;
    GtkWidget *gotoMi;
    GtkWidget *prefsMi;

    GtkWidget *viewMenu;        /* view menu      */
    GtkWidget *viewMi;
    GtkWidget *fontMi;
    GtkWidget *showtbMi;
    GSList *tbgroup = NULL;
    GtkWidget *tbvisMenu;

#ifdef HAVESOURCEVIEW
    GtkWidget *linenoMi;
    GtkWidget *linehlMi;
    GtkWidget *syntaxMi;
    GtkWidget *highlightMi;
    GtkWidget *highlightMenu;
    GtkWidget *synschemeMi;
    GtkWidget *synschemeMenu = NULL;
#endif

    GtkWidget *statusMenu;      /* status menu      */
    GtkWidget *statusMi;
    GtkWidget *clearMi;
    GtkWidget *propsMi;
    GtkWidget *brbMi;   /* (Big Red Button) */

    GtkWidget *toolsMenu;       /* tools menu      */
    GtkWidget *toolsMi;
    GtkWidget *indentMi;
    GtkWidget *unindentMi;
    GtkWidget *indfixedMi;
    GtkWidget *undfixedMi;
    GtkWidget *toupperMi;
    GtkWidget *tolowerMi;
    GtkWidget *totitleMi;

    GtkWidget *helpMenu;        /* help menu        */
    GtkWidget *helpMi;
    GtkWidget *aboutMi;

    /* create menubar, menus and submenus */
    menubar = gtk_menu_bar_new();
    fileMenu = gtk_menu_new();
    editMenu = gtk_menu_new();
    viewMenu = gtk_menu_new();
    tbvisMenu = gtk_menu_new();
    statusMenu = gtk_menu_new();
    toolsMenu = gtk_menu_new();
    helpMenu = gtk_menu_new();

    /* define filter for recentMenu for text files */
    GtkRecentFilter *filter = gtk_recent_filter_new();
    gtk_recent_filter_set_name(filter, "Text");
    gtk_recent_filter_add_mime_type(filter, "text/plain");

    /* define file menu */
    MNEMONIC_MENU_ITEM("_File", fileMi);
    IMAGE_MENU_ITEM(NEW, newMi);
    IMAGE_MENU_ITEM(OPEN, openMi);
    IMAGE_MENU_ITEM(SAVE, saveMi);
    IMAGE_MENU_ITEM(SAVE_AS, saveasMi);
    IMAGE_MENU_ITEM(CLOSE, closeMi);
    IMAGE_MENU_ITEM(QUIT, quitMi);

    SUBMENU(fileMenu,fileMi);
    APPEND_SEPARATOR(fileMenu);
    APPEND_MENU(fileMenu, newMi);
    APPEND_MENU(fileMenu, openMi);
    APPEND_SEPARATOR(fileMenu);
    APPEND_MENU(fileMenu, saveMi);
    APPEND_MENU(fileMenu, saveasMi);
    APPEND_SEPARATOR(fileMenu);
    APPEND_MENU(fileMenu, closeMi);
    APPEND_MENU(fileMenu, quitMi);
    APPEND_MENU(menubar, fileMi);

    ADD_ACCELERATOR(newMi,    GDK_n, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
    ADD_ACCELERATOR(openMi,   GDK_o, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
    ADD_ACCELERATOR(saveMi,   GDK_s, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
    ADD_ACCELERATOR(saveasMi, GDK_s, GDK_CONTROL_MASK | GDK_SHIFT_MASK, GTK_ACCEL_VISIBLE);
    ADD_ACCELERATOR(closeMi,  GDK_w, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
    ADD_ACCELERATOR(quitMi,   GDK_q, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);

    /* define edit menu */
    MNEMONIC_MENU_ITEM("_Edit", editMi);
    IMAGE_MENU_ITEM(UNDO, undoMi);
    IMAGE_MENU_ITEM(REDO, redoMi);
    IMAGE_MENU_ITEM(COPY, copyMi);
    IMAGE_MENU_ITEM(CUT, cutMi);
    IMAGE_MENU_ITEM(PASTE, pasteMi);
    IMAGE_MENU_ITEM(DELETE, deleteMi);
    IMAGE_MENU_ITEM_LABLE(ZOOM_FIT, selallMi, "_Select _All");
    IMAGE_MENU_ITEM_LABLE(CLEAR, deselectMi, "_Deselect All");
    IMAGE_MENU_ITEM(FIND, findMi);
    IMAGE_MENU_ITEM(FIND_AND_REPLACE, replaceMi);
    IMAGE_MENU_ITEM_LABLE(INDEX, gotoMi, "_Go to Line");
    IMAGE_MENU_ITEM(PREFERENCES, prefsMi);

    SUBMENU(editMenu,editMi);
    APPEND_SEPARATOR(editMenu);
    APPEND_MENU(editMenu, undoMi);
    APPEND_MENU(editMenu, redoMi);
    APPEND_SEPARATOR(editMenu);
    APPEND_MENU(editMenu, copyMi);
    APPEND_MENU(editMenu, cutMi);
    APPEND_MENU(editMenu, pasteMi);
    APPEND_MENU(editMenu, deleteMi);
    APPEND_SEPARATOR(editMenu);
    APPEND_MENU(editMenu, selallMi);
    APPEND_MENU(editMenu, deselectMi);
    APPEND_SEPARATOR(editMenu);
    APPEND_MENU(editMenu, findMi);
    APPEND_MENU(editMenu, replaceMi);
    APPEND_SEPARATOR(editMenu);
    APPEND_MENU(editMenu, gotoMi);
    APPEND_SEPARATOR(editMenu);
    APPEND_MENU(editMenu, prefsMi);
    APPEND_MENU(menubar, editMi);

    ADD_ACCELERATOR(undoMi,     GDK_z, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
    ADD_ACCELERATOR(redoMi,     GDK_z, GDK_CONTROL_MASK | GDK_SHIFT_MASK, GTK_ACCEL_VISIBLE);
    ADD_ACCELERATOR(copyMi,     GDK_c, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
    ADD_ACCELERATOR(cutMi,      GDK_x, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
    ADD_ACCELERATOR(pasteMi,    GDK_v, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
    ADD_ACCELERATOR(selallMi,   GDK_a, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
    ADD_ACCELERATOR(deselectMi, GDK_a, GDK_CONTROL_MASK | GDK_SHIFT_MASK, GTK_ACCEL_VISIBLE);
    ADD_ACCELERATOR(findMi,     GDK_f, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
    ADD_ACCELERATOR(replaceMi,  GDK_r, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
    ADD_ACCELERATOR(gotoMi,     GDK_g, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
    ADD_ACCELERATOR(prefsMi,    GDK_p, GDK_MOD1_MASK, GTK_ACCEL_VISIBLE);

    /* define view menu */
    MNEMONIC_MENU_ITEM("_View", viewMi);
    IMAGE_MENU_ITEM_LABLE(SELECT_FONT, fontMi, "_Font Selection");
    IMAGE_MENU_ITEM_LABLE(CONVERT, showtbMi, "_Show/Hide Toolbar");

    SUBMENU(viewMenu,viewMi);
    APPEND_SEPARATOR(viewMenu);
    APPEND_MENU(viewMenu, fontMi);
    APPEND_MENU(viewMenu, showtbMi);
    APPEND_MENU(menubar, viewMi);

    ADD_ACCELERATOR(viewMi,     GDK_v, GDK_MOD1_MASK, GTK_ACCEL_VISIBLE);
    ADD_ACCELERATOR(fontMi,     GDK_t, GDK_MOD1_MASK, GTK_ACCEL_VISIBLE);
    ADD_ACCELERATOR(showtbMi,   GDK_t, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);

    /* define status menu */
    /*
    MNEMONIC_MENU_ITEM("_Status", statusMi);
    IMAGE_MENU_ITEM(CLEAR, clearMi);
    IMAGE_MENU_ITEM_LABLE(MEDIA_RECORD, brbMi, "_Big Red Button...");

    SUBMENU(statusMenu,statusMi);
    APPEND_SEPARATOR(statusMenu);
    APPEND_MENU(statusMenu, clearMi);
    APPEND_MENU(menubar, statusMi);

    ADD_ACCELERATOR(clearMi, GDK_c, GDK_MOD1_MASK, GTK_ACCEL_VISIBLE);
    */

    /* define tools menu */
    MNEMONIC_MENU_ITEM("_Tools", toolsMi);
    IMAGE_MENU_ITEM_LABLE(INDENT, indentMi, "Incr_ease Indent");
    IMAGE_MENU_ITEM_LABLE(UNINDENT, unindentMi, "_Decrease Indent");
    IMAGE_MENU_ITEM_LABLE(INDENT, indfixedMi, "Indent _Fixed Width");
    IMAGE_MENU_ITEM_LABLE(UNINDENT, undfixedMi, "Unindent Fixed _Width");
    IMAGE_MENU_ITEM_LABLE(GO_UP, toupperMi, "_Uppercase");
    IMAGE_MENU_ITEM_LABLE(GO_DOWN, tolowerMi, "_Lowercase");
    IMAGE_MENU_ITEM_LABLE(GO_FORWARD, totitleMi, "_Titlecase");
    IMAGE_MENU_ITEM_LABLE(PROPERTIES, propsMi, "_Word/Char Statistics");

    SUBMENU(toolsMenu,toolsMi);
    APPEND_SEPARATOR(toolsMenu);
    APPEND_MENU(toolsMenu, indentMi);
    APPEND_MENU(toolsMenu, unindentMi);
    APPEND_SEPARATOR(toolsMenu);
    APPEND_MENU(toolsMenu, indfixedMi);
    APPEND_MENU(toolsMenu, undfixedMi);
    APPEND_SEPARATOR(toolsMenu);
    APPEND_MENU(toolsMenu, toupperMi);
    APPEND_MENU(toolsMenu, tolowerMi);
    APPEND_MENU(toolsMenu, totitleMi);
    APPEND_SEPARATOR(toolsMenu);
    APPEND_MENU(toolsMenu, toolsMi);
    APPEND_SEPARATOR(toolsMenu);
    APPEND_MENU(toolsMenu, propsMi);

    ADD_ACCELERATOR(indentMi,    GDK_i, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
    ADD_ACCELERATOR(unindentMi,  GDK_i, GDK_CONTROL_MASK | GDK_SHIFT_MASK, GTK_ACCEL_VISIBLE);
    ADD_ACCELERATOR(indfixedMi,  GDK_i, GDK_SUPER_MASK, GTK_ACCEL_VISIBLE);
    ADD_ACCELERATOR(undfixedMi,  GDK_i, GDK_SUPER_MASK | GDK_SHIFT_MASK, GTK_ACCEL_VISIBLE);
    ADD_ACCELERATOR(toupperMi,   GDK_u, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
    ADD_ACCELERATOR(tolowerMi,   GDK_u, GDK_CONTROL_MASK | GDK_SHIFT_MASK, GTK_ACCEL_VISIBLE);
    ADD_ACCELERATOR(totitleMi,   GDK_u, GDK_MOD1_MASK | GDK_SHIFT_MASK, GTK_ACCEL_VISIBLE);
    ADD_ACCELERATOR(propsMi,     GDK_r, GDK_MOD1_MASK, GTK_ACCEL_VISIBLE);

    /*define help menu */
    MNEMONIC_MENU_ITEM("_Help", helpMi);
    IMAGE_MENU_ITEM(ABOUT, aboutMi);

    SUBMENU(helpMenu,helpMi);
    APPEND_SEPARATOR(helpMenu);
    APPEND_MENU(helpMenu, aboutMi);
    APPEND_MENU(menubar, helpMi);

    ADD_ACCELERATOR(aboutMi, GDK_a, GDK_MOD1_MASK, GTK_ACCEL_VISIBLE);

    /* File Menu */
    ACTIVATE(newMi,      menu_file_new_activate);
    ACTIVATE(openMi,     menu_file_open_activate);
    ACTIVATE(saveMi,     menu_file_save_activate);
    ACTIVATE(saveasMi,   menu_file_saveas_activate);
    ACTIVATE(closeMi,    menu_file_close_activate);
    ACTIVATE(quitMi,     menu_file_quit_activate);
    /* Edit Menu */
    ACTIVATE(undoMi,     menu_edit_undo_activate);
    ACTIVATE(redoMi,     menu_edit_redo_activate);
    ACTIVATE(copyMi,     menu_edit_copy_activate);
    ACTIVATE(cutMi,      menu_edit_cut_activate);
    ACTIVATE(pasteMi,    menu_edit_paste_activate);
    ACTIVATE(deleteMi,   menu_edit_delete_activate);
    ACTIVATE(selallMi,   menu_edit_selectall_activate);
    ACTIVATE(deselectMi, menu_edit_deselectall_activate);
    ACTIVATE(findMi,     menu_edit_find_activate);
    ACTIVATE(replaceMi,  menu_edit_replace_activate);
    ACTIVATE(gotoMi,     menu_edit_goto_activate);
    ACTIVATE(prefsMi,    menu_edit_preferences_activate);
    /* View Menu */
    ACTIVATE(fontMi,     menu_font_select_activate);
    ACTIVATE(showtbMi,   menu_showtb_activate);
    /* Status Menu */
    //ACTIVATE(clearMi,    menu_status_clear_activate);
    //ACTIVATE(propsMi,    menu_status_properties_activate);
    //ACTIVATE(brbMi,      menu_status_bigredbtn_activate);
    /* Tools Menu */
    ACTIVATE(indentMi,   menu_tools_indent_activate);
    ACTIVATE(unindentMi, menu_tools_unindent_activate);
    ACTIVATE(indfixedMi, menu_tools_indent_fixed_activate);
    ACTIVATE(undfixedMi, menu_tools_unindent_fixed_activate);
    ACTIVATE(toupperMi,  menu_tools_toupper_activate);
    ACTIVATE(tolowerMi,  menu_tools_tolower_activate);
    ACTIVATE(totitleMi,  menu_tools_totitle_activate);
    /* Help Menu */
    ACTIVATE(aboutMi,    menu_help_about_activate);

    return (app->menubar = menubar);
}

/*
 * menu callback functions
 *
 *  _File menu
 */
void menu_file_new_activate(GtkMenuItem *menuitem, FunnyWindow *app) {   /* TODO: fork/execv, create new window and process */
    buffer_clear(app);         /* check for save and clear  */
    status_set_default(app);   /* statusbard default values */
}

void menu_file_open_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
    gchar *newfile = get_open_filename(app);

    if (!newfile) return;

    buffer_clear(app);             /* check for save and clear  */
    app->filename = newfile;        /* assign new filename */
    split_fname(app);              /* decompose name into components */
    buffer_insert_file(app, NULL); /* insert file in buffer */

    // buffer_file_open_dlg (app, NULL);   /* insert file */
}

void menu_file_save_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
//     if (app->filename == NULL)
//     {
//         app->filename = get_save_filename (app);
//         if (app->filename != NULL) buffer_write_file (app, NULL);
//     }
//     else
    buffer_save_file(app, NULL);

    if (menuitem) {}
}

void menu_file_saveas_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
    gchar *filename = NULL;

    filename = get_save_filename(app);
    if (filename)
        buffer_save_file(app, filename);
    else
        dialog_info("Warning: Save of File Canceled!", "SAVE CANCELED!");

    /* do not g_free filename, it is used as app->filename and freed
     * elsewhere in the code as the filename changes or program exits.
     */

    /* fix, just get filename and handle free of app->filename */
//     app->filename = get_save_filename (app);
//     if (app->filename != NULL) buffer_write_file (app, NULL);

}

void menu_file_close_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
    buffer_clear(app);     /* check for save and clear */
}

void menu_file_quit_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
    /* get window size */
    gtk_window_get_size(GTK_WINDOW (app->window), &(app->winwidth), &(app->winheight));

    /* check changed, prompt yes/no, apply buffer cleanups */
    buffer_handle_quit(app);
    gtk_main_quit();
}

/*
 *  _Edit menu
 */
void menu_edit_undo_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
#ifdef HAVESOURCEVIEW
    if (gtk_source_buffer_can_undo (app->buffer))
        gtk_source_buffer_undo (app->buffer);
    else
        err_dialog ("Error:\n\nUnable to undo previous operation.");
#endif
    gtkwrite_window_set_title(NULL, app);
}

void menu_edit_redo_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
#ifdef HAVESOURCEVIEW
    if (gtk_source_buffer_can_redo (app->buffer))
        gtk_source_buffer_redo (app->buffer);
    else
        err_dialog ("Error:\n\nUnable to redo previous operation.");
#endif
}

void menu_edit_copy_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
    GtkTextBuffer *buffer;
    GtkClipboard *clipboard;

    clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
    buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW (app->view));
    gtk_text_buffer_copy_clipboard(buffer, clipboard);
}

void menu_edit_cut_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
    GtkTextBuffer *buffer;
    GtkClipboard *clipboard;

    clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
    buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW (app->view));
    gtk_text_buffer_cut_clipboard(buffer, clipboard, TRUE);
}

void menu_edit_paste_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
    GtkTextBuffer *buffer;
    GtkClipboard *clipboard;

    clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
    buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW (app->view));
    gtk_text_buffer_paste_clipboard(buffer, clipboard, NULL, TRUE);
}

void menu_edit_delete_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
    GtkTextBuffer *buffer;

    buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW (app->view));
    gtk_text_buffer_delete_selection(buffer, FALSE, TRUE);
}

void menu_edit_selectall_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
    buffer_select_all(app);
}

void menu_edit_deselectall_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
    buffer_deselect_all(app);
}

void menu_edit_find_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
    //create_find_dlg (app);
    //if (menuitem) {}
    //if (app) {}
}

void menu_edit_replace_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
    //create_replace_dlg (app);
    //if (menuitem) {}
    //if (app) {}
}

void menu_edit_goto_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
    //create_goto_dlg (app);
    // g_print ("menu_edit_goto_activate callback\n");
    //if (menuitem) {}
    // if (app) {}
}

void menu_edit_preferences_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
    status_update_str(app, "Edit : Preferences");
    create_settings_dialog(app);
}

/*
 *  _View menu
 */
void menu_font_select_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
    font_select_dialog(GTK_WIDGET (menuitem), app);
}

void menu_showtb_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
    app->showtoolbar = gtk_widget_get_visible(app->toolbar) ? FALSE : TRUE;
    gtk_widget_set_visible(app->toolbar, app->showtoolbar);
    gtk_widget_set_sensitive(app->toolbarMenus, app->showtoolbar);
//     gtk_widget_set_visible (app->toolbar,
//                             gtk_widget_get_visible (app->toolbar) ?
//                             FALSE : TRUE);
}

/*
 *  _Status menu
 */
void menu_status_clear_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
    status_pop(GTK_WIDGET (menuitem), app);
}

void menu_status_properties_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
    // propcb (GTK_WIDGET (menuitem), app);
    // dlg_info ("Currently unused test callback.", "Unused Test Callback");
    buffer_content_stats(GTK_TEXT_BUFFER(app->buffer));
}

void menu_status_bigredbtn_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
    status_pop(GTK_WIDGET (menuitem), app);
    // selection_dump (GTK_TEXT_BUFFER(app->buffer), dump2lower);
    /* default if nothing being tested */
    err_dialog("Note:  Noting currently hooked to Big Red Button...");
}

/*
 *  _Tools menu
 */
void menu_tools_indent_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
    status_pop(GTK_WIDGET (menuitem), app);
    GtkTextIter start, end;
    gtk_text_buffer_get_selection_bounds(GTK_TEXT_BUFFER(app->buffer), &start, &end);
    buffer_indent_lines(app, &start, &end);
}

void menu_tools_unindent_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
    status_pop(GTK_WIDGET (menuitem), app);
    GtkTextIter start, end;
    gtk_text_buffer_get_selection_bounds(GTK_TEXT_BUFFER(app->buffer), &start, &end);
    buffer_unindent_lines(app, &start, &end);
}

void menu_tools_indent_fixed_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
    status_pop(GTK_WIDGET (menuitem), app);
    GtkTextIter start, end;
    gtk_text_buffer_get_selection_bounds(GTK_TEXT_BUFFER(app->buffer), &start, &end);
    buffer_indent_lines_fixed(app, &start, &end);
}

void menu_tools_unindent_fixed_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
    status_pop(GTK_WIDGET (menuitem), app);
    GtkTextIter start, end;
    gtk_text_buffer_get_selection_bounds(GTK_TEXT_BUFFER(app->buffer), &start, &end);
    buffer_unindent_lines_fixed(app, &start, &end);
}

void menu_tools_toupper_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
    selection_for_each_char(GTK_TEXT_BUFFER(app->buffer), str2upper);
}

void menu_tools_tolower_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
    selection_for_each_char(GTK_TEXT_BUFFER(app->buffer), str2lower);
}

void menu_tools_totitle_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
    selection_for_each_char(GTK_TEXT_BUFFER(app->buffer), str2title);
}

/*
 *  _Help menu
 */
void menu_help_about_activate(GtkMenuItem *menuitem, FunnyWindow *app) {
    help_about(app);
}

/*
 * general window functions
 */
void help_about(FunnyWindow *app) {
    static const gchar *const authors[] = {
            "fanguangping <fanguangping@163.com>",
            NULL
    };

    static const gchar copyright[] = \
            "Copyright \xc2\xa9 2017 fanguangping";

    // static const gchar comments[] = "GTKedit Text Editor";
    static const gchar comments[] = "Funny Editor";

    gtk_show_about_dialog(GTK_WINDOW (app->window),
                          "authors", authors,
                          "comments", comments,
                          "copyright", copyright,
                          "version", "0.3.0",
                          "website", "",
                          "program-name", "Funny Editor",
                          "logo-icon-name", GTK_STOCK_EDIT,
                          NULL);
}
